package view;

import java.io.BufferedReader; 
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import model.Constants;


import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import view.listener.CloseButtonListener;
import view.listener.StartButtonListener;
import control.Storage;
import control.Util;

public class ConfigFrame {

	private Display display;
	private Shell shell;
	private Label playerName, enemyName;
	private Text playerNameField, enemyNameField;
	private Button start, close;
	private Label sizeName;
	private Text sizeNameField;
	public class loadIMG implements Runnable{

		@Override
		public void run() {
			try {

				URL url = new URL(
						"http://api.flickr.com/services/feeds/photos_public.gne?tags=cat");
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setRequestMethod("GET");
				conn.setRequestProperty("Accept", "application/json");

				if (conn.getResponseCode() != 200) {
					throw new RuntimeException("Failed : HTTP error code : "
							+ conn.getResponseCode());
				}

				BufferedReader br = new BufferedReader(new InputStreamReader(
						(conn.getInputStream())));

				String output;
				System.out.println("Output from Server .... \n");
				while ((output = br.readLine()) != null) {
					if (output.contains("<link") && output.contains(".jpg")) {
						for (String s : output.split("\"")) {
							s = s.trim();
							if (s.startsWith("http")) {


								Storage.getImageList().add(
										ImageDescriptor.createFromURL(new URL(s)).createImage());
								System.out.println(s);
							}
						}
						;
					}

				}

				conn.disconnect();

			} catch (MalformedURLException e) {

				e.printStackTrace();

			} catch (IOException e) {

				e.printStackTrace();

			}

			
		}}
	public ConfigFrame() {
		Thread t1 = new Thread(new loadIMG());
		t1.start();
		display = new Display();
		shell = new Shell(display);
		configureShell();
		keepAlive();
	}

	private void buildContent() {

		playerName = new Label(shell, SWT.NULL);
		playerName.setText(Constants.PLAYERNAMELABEL);

		playerNameField = new Text(shell, SWT.BORDER);
		playerNameField.setLayoutData(new GridData(100, 15));

		enemyName = new Label(shell, SWT.NULL);
		enemyName.setText(Constants.ENEMYNAMELABEL);

		enemyNameField = new Text(shell, SWT.BORDER);
		enemyNameField.setLayoutData(new GridData(100, 15));
		

		sizeName = new Label(shell, SWT.NULL);
		sizeName.setText(Constants.SIZELABEL);

		sizeNameField = new Text(shell, SWT.BORDER);
		sizeNameField.setLayoutData(new GridData(100, 15));
		
		start = new Button(shell, SWT.PUSH);
		start.setText(Constants.STARTBUTTONLABEL);
		start.addSelectionListener(new StartButtonListener());

		close = new Button(shell, SWT.PUSH);
		close.setText(Constants.CLOSEBUTTONLABEL);
		close.addSelectionListener(new CloseButtonListener());

	}

	private void configureShell() {
		shell.setLayout(getShellLayout());
		shell.setText(Constants.SHELLTITLE);
		buildContent();
		shell.pack();
		Util.centerFrame(shell);
		shell.setVisible(true);
	}

	private Layout getShellLayout() {
		GridLayout layout = new GridLayout(2, false);
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		return layout;
	}

	private void keepAlive() {

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}
}
